<template>
  <div class="main-container">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="250px" class="sidebar">
        <div class="sidebar-header">
          <div class="user-info">
            <el-avatar :size="40" class="user-avatar">
              {{ user?.name?.charAt(0) || 'U' }}
            </el-avatar>
            <div class="user-details">
              <div class="user-name">{{ user?.name || '用户' }}</div>
              <div class="user-status">在线</div>
            </div>
          </div>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          @select="handleMenuSelect"
        >
          <el-menu-item index="/main/messages">
            <el-icon><Message /></el-icon>
            <span>消息中心</span>
            <el-badge
              v-if="unreadCount > 0"
              :value="unreadCount"
              class="message-badge"
            />
          </el-menu-item>
          
          <el-menu-item index="/main/settings">
            <el-icon><Setting /></el-icon>
            <span>设置</span>
          </el-menu-item>
        </el-menu>
        
        <div class="sidebar-footer">
          <el-button
            type="text"
            @click="handleMinimize"
            class="action-button"
          >
            <el-icon><Minus /></el-icon>
            最小化到托盘
          </el-button>
          
          <el-button
            type="text"
            @click="handleLogout"
            class="action-button logout-button"
          >
            <el-icon><SwitchButton /></el-icon>
            退出登录
          </el-button>
        </div>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
    
    <!-- 连接状态指示器 -->
    <div class="connection-status" :class="{ connected: isConnected }">
      <el-icon>
        <component :is="isConnected ? 'Connection' : 'Close'" />
      </el-icon>
      <span>{{ isConnected ? '已连接' : '未连接' }}</span>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import { Message, Setting, Minus, SwitchButton, Connection, Close } from '@element-plus/icons-vue'
import { useAuthStore } from '../stores/auth'
import { useMessageStore } from '../stores/message'
import { useSignalRStore } from '../stores/signalr'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()
const messageStore = useMessageStore()
const signalRStore = useSignalRStore()

const user = computed(() => authStore.user)
const unreadCount = computed(() => messageStore.unreadCount)
const isConnected = computed(() => signalRStore.isConnected)
const activeMenu = computed(() => route.path)

const handleMenuSelect = (index) => {
  router.push(index)
}

const handleMinimize = async () => {
  if (window.electronAPI) {
    await window.electronAPI.minimizeToTray()
  }
}

const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要退出登录吗？',
      '确认退出',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    await authStore.logout()
    ElMessage.success('已退出登录')
  } catch (error) {
    // 用户取消
  }
}

onMounted(() => {
  // 初始化消息存储
  messageStore.init()
})

// 监听未读消息数变化，更新角标
watch(unreadCount, (newCount) => {
  if (window.electronAPI) {
    window.electronAPI.setBadgeCount(newCount)
  }
}, { immediate: true })
</script>

<style lang="scss" scoped>
.main-container {
  height: 100vh;
  position: relative;
}

.sidebar {
  background: $sidebar-bg;
  border-right: 1px solid $border-color;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid $border-color;
  
  .user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    
    .user-avatar {
      background: $primary-color;
      color: white;
      font-weight: 600;
    }
    
    .user-details {
      flex: 1;
      
      .user-name {
        font-size: 14px;
        font-weight: 500;
        color: $text-color-primary;
        margin-bottom: 2px;
      }
      
      .user-status {
        font-size: 12px;
        color: $success-color;
      }
    }
  }
}

.sidebar-menu {
  flex: 1;
  border: none;
  
  .el-menu-item {
    position: relative;
    
    .message-badge {
      position: absolute;
      right: 15px;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}

.sidebar-footer {
  padding: 20px;
  border-top: 1px solid $border-color;
  
  .action-button {
    width: 100%;
    justify-content: flex-start;
    margin-bottom: 8px;
    color: $text-color-regular;
    
    &:hover {
      background: $hover-bg;
    }
    
    &.logout-button {
      color: $danger-color;
      
      &:hover {
        background: rgba($danger-color, 0.1);
      }
    }
  }
}

.main-content {
  padding: 0;
  background: $bg-color;
}

.connection-status {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  border-radius: 6px;
  font-size: 12px;
  z-index: 1000;
  
  &.connected {
    background: rgba($success-color, 0.9);
  }
  
  .el-icon {
    font-size: 14px;
  }
}
</style>